<template>
  <div class="header">
    <div class="page-title">卡密管理</div>
    <el-button
      class="m-t-15"
      type="primary"
      size="small"
      icon="el-icon-edit"
      style="margin-right:20px"
      @click="handleCCreate"
      >批量生成</el-button
    >
    
    状态：
    <el-select
      v-model="status"
      placeholder="请选择"
      style="margin-right: 30px"
    >
      <el-option
        v-for="item in statusList"
        :key="item.value"
        :label="item.name"
        :value="item.value"
      >
      </el-option>
    </el-select>
    <el-button @click="search">搜索</el-button>
    <el-button @click="exportData" type="primary">导出</el-button>
    <el-table :data="tableData" class="m-t-5">
      <el-table-column type="selection" width="55"></el-table-column>
      <el-table-column prop="id" label="id" width="100"></el-table-column>
      <el-table-column prop="code" label="兑换码" ></el-table-column>
      <el-table-column prop="price" label="面值" ></el-table-column>
      <el-table-column prop="create_time" label="创建时间" ></el-table-column>
      <el-table-column prop="status_text" label="状态" ></el-table-column>
    </el-table>
    <el-pagination
      background
      hide-on-single-page
      layout="prev, pager, next"
      :total="total"
      :current-page="current"
      @current-change="changePage"
    >
    </el-pagination>
  </div>
  <el-dialog title="卡密生成" v-model="dialogFormVisible" width="35%">
    <el-form>
      <el-form-item label="生成数量">
        <el-input
          v-model.number="form.number"
          autocomplete="off"
          placeholder="请输入数量"
        ></el-input>
      </el-form-item>
      <el-form-item label="面值">
        <el-input
          v-model="form.price"
          autocomplete="off"
          placeholder="请输入面值"
        ></el-input>
      </el-form-item>
    </el-form>
    <template v-slot:footer>
      <div class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleSubmitForm">确 定</el-button>
      </div>
    </template>
  </el-dialog>
</template>
<script>
import CONST_DMOMAIN_URL from "@/config/env";
import { request_code_list, request_build_code } from "@/util/api";

export default {
  name: "SeekType",
  data() {
    return {
      current: 1,
      total: 0,
      form: {
        number:10,
        price:10,
      },
      dialogFormVisible: false,
      actionType: 0, // 1 添加  2 编辑
      status: 0,
      statusList: [
        {name: '全部', value: null},
        {name: '未领取', value: 0},
        {name: '已使用', value: 1},
        {name: '未使用', value: 2},
      ]
    };
  },
  computed: {},
  mounted() {
    this.init();
  },
  methods: {
    exportData() {
        let url = CONST_DMOMAIN_URL + '/admin/';
        console.log(url)
    },
    init() {
      this.$nextTick(() => {
        request_code_list()
          .then((res) => {
            const result = res.data;
            if (result.code == 200) {
              this.tableData = result.data.data;
              this.total = result.data.total;
              this.current = result.data.current;
            }
          })
          .catch();
      });
    },
    // 添加
    handleCCreate() {
      this.actionType = 1;
      this.form = {};
      this.dialogFormVisible = true;
    },
    // 提交
    handleSubmitForm() {
      let apiFunc;
      switch (this.actionType) {
        case 1: // 添加
            apiFunc = request_build_code; 
          break;
        default:
          this.$message.error({
            message: "操作类型不正确",
            offset: 70,
          });
          return false;
      }
      apiFunc({
        ...this.form,
      }).then((res) => {
        const result = res.data;
        if (result.code == 200) {
          this.dialogFormVisible = false;
          this.$notify.success({
            title: "系统提示",
            message: result.message,
            offset: 70,
          });
          this.changePage(1);
        } else {
          this.$message.error({
            message: result.message,
            offset: 70,
          });
        }
      });
    },
    search() {
      this.changePage(1);
    },
    // 分页
    changePage(page) {
      request_code_list({
        page,
        status: this.status
      })
        .then((res) => {
          const result = res.data;
          if (result.code == 200) {
            this.tableData = result.data.data;
            this.total = result.data.total;
            this.current = result.data.current;
          }
        })
        .catch();
    },
  },
};
</script>

<style scoped  lang="less">
</style>